package com.gengealogy.net.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gengealogy.net.po.User;
import com.gengealogy.net.service.UserService;


@Controller
public class UserController {

	@Autowired
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * 用户登录
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String username, String password, Model model, HttpSession session) {
		// 通过账号和密码查询用户
		User user = userService.findUser(username, password);
		if (user != null) {
			// 将用户对象添加到Session
			int user_id = user.getUser_id();
			int user_ident = user.getIdent();
			session.setAttribute("USER_ID", user_id);
			session.setAttribute("USER_IDENT", user_ident);
			session.setAttribute("USER_SESSION", user);
			if (user.getIdent() == 0)
				// 跳转到普通用户主页面
				return "redirect:user/userAddClan.action";
			if (user.getIdent() == 1)
				// 跳转到族谱编纂人员主页面
				return "redirect:build/getClan.action";
			if (user.getIdent() == 2)
				// 跳转到管理员主页面
				return "redirect:admin/getUser.action";
		}
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
		// 返回到登录页面
		return "login";
	}

	/**
	 * 向用户登陆页面跳转
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String toLogin() {
		return "login";
	}

	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/logout.action")
	public String logout(HttpSession session) {
		// 清除Session
		session.invalidate();
		// 重定向到登录页面的跳转方法
		return "redirect:login.action";
	}

	/**
	 * 向用户注册页面跳转
	 */
	@RequestMapping(value = "/register.action", method = RequestMethod.GET)
	public String toRegister() {
		return "register";
	}

	@RequestMapping(value = "/register.action", method = RequestMethod.POST)
	public String register(User user, Model model) {
		int rows=userService.register(user);
	    if(rows > 0){			
	    	return "login";
	    }else{
	    	model.addAttribute("msg", "注册失败，请重新注册！");
			return "register";		
	    }
		
	}

	/**
	 * 管理員更新用户信息
	 */

	@RequestMapping(value = "/admin/update.action", method = RequestMethod.POST)
	@ResponseBody
	public String updateUser(User user, Model model) {
		int rows =userService.updateUser(user);
		if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}
	
	/**
	 * 删除用户信息
	 */
	@RequestMapping("/deleteUser.action")
	@ResponseBody
	public String deleteUser(int user_id, Model model) {
	    int rows = userService.deleteUser(user_id);
	    if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}
	
	/**
	 * 创建管理员分类
	 */
	@RequestMapping("/create.action")
	@ResponseBody
	public String adminCreate(User user,HttpSession session) {

	    // 执行Service层中的创建方法，返回的是受影响的行数
	    int rows = userService.register(user);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
	

	/**
	 * 向管理员首页跳转
	 */
	@RequestMapping(value = "/admin/getUser.action", method = RequestMethod.GET)
	public String getUser(Model model) {
		List<User> users=userService.getUser();
		model.addAttribute("users", users);
		return "admin/admin_index";
	}
	
	/**
	 * 通过id获取用户信息
	 */
	@RequestMapping("/getUserById.action")
	@ResponseBody
	public User getUsersById(Integer user_id) {
		User users = userService.getUserById(user_id);
	    return users;
	}
	
	/**
	 * 跳转到个人信息页面
	 */

	@RequestMapping(value = "/admin/editUser.action", method = RequestMethod.GET)
	public String editUser(Model model, HttpSession session) {
		int user_id=(int) session.getAttribute("USER_ID");
		User user=userService.getUserById(user_id);
		model.addAttribute("user",user);
		if (user.getIdent() == 0)
			// 跳转到普通用户主页面
			return "user/user_myedit";
		if (user.getIdent() == 1)
			// 跳转到族谱编纂人员主页面
			return "build/build_edit";
		if (user.getIdent() == 2)
			// 跳转到管理员主页面
			return "admin/admin_edit";
		model.addAttribute("msg", "操作错误！");
		// 返回到登录页面
		return "redirect:admin/getUser.action";
	}
	
	
	/**
	 * 个人信息修改
	 */

	@RequestMapping(value = "/user/update.action", method = RequestMethod.POST)
	@ResponseBody
	public String userUpdate(User user, Model model) {
		int rows =userService.userUpdate(user);
		if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}
}
